package icu.feigroup.infrastructure.dcc;

import icu.feigroup.types.annotation.DCCValue;
import icu.feigroup.types.common.Constants;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * projectName:icu.feigroup.infrastructure.dcc
 *
 * @author 飞
 * @since 2025/2/22 14:04
 * description:
 */
@Service
public class DCCService {
    /**降级*/
    @DCCValue("downgradeSwitch:0")
    private String downgradeSwitch;

    /**切量*/
    @DCCValue("cutRange:100")
    private String cutRange;

    /** 白名单功能*/
    @DCCValue("scBlacklist:s01c01")
    private String scBlacklist;

    public boolean isDowngradeSwitch(){
        return "1".equals(downgradeSwitch);
    }

    public boolean isCutRange(String userId){
        //哈希值有负的
        int hashCode = Math.abs(userId.hashCode());
        //取最后两位
        int lastTwoDigit = hashCode % 100;

        return lastTwoDigit<=Integer.parseInt(cutRange);
    }

    public boolean isSCBlackIntercept(String source, String channel) {
        List<String> list = Arrays.asList(scBlacklist.split(Constants.SPLIT));
        return list.contains(source + channel);
    }
}
